﻿# -*- coding: utf-8 -*-
"""
Created on Sat Jul  7 11:51:59 2018

@author: S103878
"""

import requests
import json
import csv
import random
from datetime import datetime
import time


class Producs_detail(object):
    def __init__(self,pathfile):
        self.pathfile = pathfile
        datenw = datetime.now().strftime('%Y%m%d%H%M')
        self.filename = '{}_{}.csv'.format(self.pathfile[10:15], datenw)
        self.get_file()
        
    def get_file(self):
        '''创建一个含有标题的表格'''
        title = ['item_id', 'time', 'sickness', '材质成分', '袖长', '货号', '销售渠道类型', 
                 '领型', '颜色', '尺码', '品牌', '版型', '适用场景', '基础风格', 'title', 
                 'commentCount', 'subtitle', 'price', 'extraPrices', 'MonsoldCount', 'img', 
                 'startTime', 'endTime', 'NowsoldCount']
        with open(self.filename,'w',newline='') as f:
            writer = csv.DictWriter(f,fieldnames=title)
            writer.writeheader()
        return 'ok'
    
    def get_url(self, pathfile):
        with open(pathfile, newline='') as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                yield 'https://'+row['url']
                
    def set_resquests(self, url):
        thatid = url[-12:]
        datenum = int((datetime.now()-datetime.strptime('1970-01-01 00:00:00','%Y-%m-%d %H:%M:%S')).total_seconds()*1000)
        headers = { 'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
            'Referer': 'https://detail.m.tmall.com/item.htm?id={}'.format(thatid)}
        get_url =  '''https://h5api.m.taobao.com/h5/mtop.taobao.detail.getdetail/6.0/?\
jsv=2.4.8&appKey=12574478&t={0}&sign=c5aca960613336cd613ca34284\
6fb95a&api=mtop.taobao.detail.getdetail&v=6.0&dataType=jsonp&ttid\
=2017%40taobao_h5_6.6.0&AntiCreep=true&type=jsonp&callback=mtopjs\
onp2&data=%7B%22itemNumId%22%3A%22{1}%22%7D'''.format(datenum, thatid)
        with requests.Session() as s:
            s.get(url, headers=headers)
            res = s.get(get_url, headers=headers)
            time.sleep(3+random.random())
        return json.loads(res.text[11:-1])


    def basic_info(self, json_data):
        basic_info = json_data['data']['props']['groupProps'][0]['基本信息']
        basic_value = []
        for i in basic_info:
            basic_value.append(list(i.values())[0])
        return basic_value
    
    def get_monsellcounts(self, json_data):
        item_basic = json_data['data']['item']
        return item_basic['title'], item_basic['commentCount'], item_basic['subtitle']
    
    
    def get_sell_price(self, json_data):
        sell_counts = json.loads(json_data['data']['apiStack'][0]['value'])
        transmitPrice = sell_counts['price']['transmitPrice']['priceText']
        try:
            oprice = sell_counts['price']['extraPrices'][0]['priceText']
        except KeyError as e:
            oprice = ''
        try:
            sellCount = sell_counts['item']['sellCount']
        except KeyError as e:
            sellCount = ''
        try:
            sell_img = sell_counts['item']['videos'][0]['videoThumbnailURL']
        except IndexError as e:
            sell_img = 'https:'+sell_counts['skuBase']['props'][0]['values'][0]['image']
        except KeyError as e:
            sell_img = ''
        return transmitPrice, oprice, sellCount, sell_img
    
    
    def get_time_count(self, json_data):
        sell_counts = json.loads(json_data['data']['apiStack'][0]['value'])
        try:
            startTime = sell_counts['vertical']['jhs']['startTime']
            endTime = sell_counts['vertical']['jhs']['endTime']
            soldCount = sell_counts['vertical']['jhs']['soldCount']
        except KeyError as e:
            startTime = endTime = soldCount = ''
        return startTime, endTime, soldCount
    
    def main(self):
        title = ['item_id', 'time', 'sickness', '材质成分', '袖长', '货号', '销售渠道类型', 
                 '领型', '颜色', '尺码', '品牌', '版型', '适用场景', '基础风格', 'title', 
                 'commentCount', 'subtitle', 'price', 'extraPrices', 'MonsoldCount', 'img', 
                 'startTime', 'endTime', 'NowsoldCount']
        for url in self.get_url(self.pathfile):
            try:
                print(url[-12:], '正在写入数据中')
                json_data = self.set_resquests(url)
                one = []
                one.append(url[-12:])
                basic_value = self.basic_info(json_data)
                title, commentCount, subtitle = self.get_monsellcounts(json_data)
                transmitPrice, oprice, sellCount, sell_img = self.get_sell_price(json_data)
                startTime, endTime, soldCount = self.get_time_count(json_data)
                one.extend([title, commentCount, subtitle, transmitPrice, oprice, sellCount, sell_img, startTime, endTime, soldCount])
                one.extend(basic_value)
                with open(self.filename, 'a') as f:
                    writer = csv.writer(f)
                    writer.writerows([one])
            except Exception as e:
                print(url[-12:], 'wrong')
                continue
            finally:
                print(url[-12:], 'is over\n')
                
if __name__ == '__main__':
    pathfile = r"C:\Users\S103878\Pictures\a21fs_201807091731.csv"
    tm = Producs_detail(pathfile)
    tm.main()
    print('all is ok')